package com.wcl.demo.ui.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.wcl.demo.R;
import com.wcl.demo.ui.activity.BaseActivity;
import com.wcl.demo.ui.adapter.BlackListAdapter;
import com.wcl.demo.utils.ActivityUtils;
import com.wcl.demo.view.CustomDialog;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by dllo on 15/12/3.
 */
public class BlackListActivity extends BaseActivity {

    private String groupid;
    private List<String> list;
    private BlackListAdapter adapter;

    @Bind(R.id.blacklist_list)
    ListView blacklist_list;

    @OnClick(R.id.blacklist_back)
    void blacklist_back(View v) {
        finish();
    }

    @Override
    protected int getLayout() {
        return R.layout.activtiy_blacklist;
    }

    @Override
    protected void initView() {
        Bundle bundle = ActivityUtils.fromSrc(this);
        this.groupid = bundle.getString("groupid");
        adapter = new BlackListAdapter();
        blacklist_list.setAdapter(adapter);

        blacklist_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {


                CustomDialog.Builder builder = new CustomDialog.Builder(parent.getContext());
                builder.setMessage("移除黑名单");
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        try {
                            EMGroupManager.getInstance().unblockUser(groupid, list.get(position));//需异步处理
                        } catch (EaseMobException e) {
                            e.printStackTrace();
                        }
                        refresh();

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();

                return false;
            }
        });
    }

    @Override
    protected void initData() {

        try {
            list = EMGroupManager.getInstance().getBlockedUsers(groupid);//需异步处理
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
        adapter.addData(list);
        adapter.notifyDataSetChanged();

    }

    private void refresh() {
        initData();
    }
}
